/**
 * DWR remote call data store
 *
 * @param config
 *            {Object}
 *
 * <pre>
 * dwr {Object}  See example -- support method:load/save/update and params
 * fields {Array} See example
 *
 * Example :
 * var dwrDs = new SHED.data.DWRStore({
 *     dwr: {
 *         call: dwr.method,
 *         save: dwr.method,
 *         update: dwr.method,
 *         params: 'xxx' || ['aaa', 'bbb']
 *     },
 *     fields: [{
 *         name: 'staffId',
 *         type: 'int'
 *     }, 'staffName', {
 *         name: 'sex',
 *         type: 'int'
 *     }]
 * });
 * </pre>
 *
 */
SHED.data.DWRStore = function(config) {
    /**
     * @cfg {dwr} SHED.data.DWRProxy
     * @cfg {fields} config of Ext.data.Record
     *
     * <pre>
     *    ['fd1', 'fd2', {
     *        name: '',
     *        type: '',
     *        mapping: ''
     *    }, {
     *        name: '',
     *        type: '',
     *        mapping: ''
     *    }]
     * </pre>
     */
    Ext.apply(this, config);
    if (!this.proxy && this.dwr) {
        this.dwr.ownerCt = config.ownerCt;
        this.proxy = new SHED.data.DWRProxy(this.dwr);
    }
    if (!this.reader) {
        this.reader = new SHED.data.DWRReader(this.fields || ['key', 'label'], this.defaultItems ? {
            defaultItems: this.defaultItems
        } : null);
    }
    
    this.addEvents({
        beforesave: true,
        save: true,
        saveexception: true
    });
    
    SHED.data.DWRStore.superclass.constructor.call(this);
};
Ext.extend(SHED.data.DWRStore, Ext.data.Store, {
    save: function(options) {
        options = options ? options : {};
        if (this.fireEvent("beforesave", this, options) !== false) {
            this.storeOptions(options);
            var p = Ext.apply(options.params ? options.params : {}, this.baseParams);
            this.proxy.save(p, this.saveRecords, this, options);
        }
    },
    
    saveRecords: function(o, options, success) {
        if (!o || success === false) {
            if (success !== false) {
                this.fireEvent("save", this, {}, options);
            }
            if (options.callback) {
                options.callback.call(options.scope || this, [], options, false);
            }
            return;
        }
        
        this.data.clear();
        this.data.add(o);
        this.totalLength = 1;
        
        this.fireEvent("save", this, o, options);
        if (options.callback) {
            options.callback.call(options.scope || this, o, options, true);
        }
    },
    
    update: function(options) {
        options = options ? options : {};
        if (this.fireEvent("beforesave", this, options) !== false) {
            this.storeOptions(options);
            var p = Ext.apply(options.params ? options.params : {}, this.baseParams);
            this.proxy.update(p, this.updateRecords, this, options);
        }
    },
    
    updateRecords: function(o, options, success) {
        if (!o || success === false) {
            if (success !== false) {
                this.fireEvent("save", this, {}, options);
            }
            if (options.callback) {
                options.callback.call(options.scope || this, [], options, false);
            }
            return;
        }
        
        this.data.clear();
        this.data.add(o);
        this.totalLength = 1;
        
        this.fireEvent("save", this, o, options);
        if (options.callback) {
            options.callback.call(options.scope || this, o, options, true);
        }
    }
});
